// $Id: transport.c 88 2011-10-17 18:00:11Z cedric.shih@gmail.com $
/*
 * Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "log.h"

#include "transport.h"

const char *niortp_transport_type_to_str(enum niortp_transport_type type)
{
	switch (type) {
	case NIORTP_TRANSPORT_UDP_UNICAST:
		return "rtp";
	case NIORTP_TRANSPORT_INTERLEAVED:
		return "rtsp";
	default:
		return "(unknown)";
	}
}

struct niortp_transport *niortp_transport_map_find(
		struct niortp_transport_map *map, unsigned char payload_type,
		enum niortp_transport_type type)
{
	struct niortp_transport_entry find, *found;

	niortsp_debug("finding transport: payload type=%d, transport type=%s",
			payload_type, niortp_transport_type_to_str(type));

	find.payload_type = payload_type;
	find.type = type;

	if ((found = RB_FIND(niortp_transport_map, map, &find))) {
		return &found->transporter;
	} else {
		return NULL;
	}
}

int niortp_transport_map_add(struct niortp_transport_map *map,
		unsigned char payload_type, enum niortp_transport_type type,
		struct niortp_transport *transporter)
{
	int rc;
	struct niortp_transport_entry *entry;

	if (niortp_transport_map_find(map, payload_type, type)) {
		rc = EEXIST;
		niortsp_error("already registered: payload type=%d, "
				"transport type=%s", payload_type,
				niortp_transport_type_to_str(type));
		goto finally;
	}

	niortsp_debug("adding transport: payload type=%d, transport type=%s",
			payload_type, niortp_transport_type_to_str(type));

	if (!(entry = (struct niortp_transport_entry *)calloc(
			1, sizeof(*entry)))) {
		rc = errno ? errno : -1;
		niortsp_error("calloc: %s", strerror(rc));
		goto finally;
	}

	entry->payload_type = payload_type;
	entry->type = type;
	memcpy(&entry->transporter, transporter, sizeof(entry->transporter));
	RB_INSERT(niortp_transport_map, map, entry);
	rc = 0;

finally:
	return rc;
}

void niortp_transport_map_remove(struct niortp_transport_map *map,
		struct niortp_transport_entry *entry)
{
	niortsp_debug("removing transport: %p", entry);

	RB_REMOVE(niortp_transport_map, map, entry);

	free(entry);
}

void niortp_transport_map_clear(struct niortp_transport_map *map)
{
	struct niortp_transport_entry *entry;

	while ((entry = RB_MIN(niortp_transport_map, map))) {
		niortp_transport_map_remove(map, entry);
	}
}

int niortp_transporter_compare(struct niortp_transport_entry *a,
		struct niortp_transport_entry *b)
{
	int result = a->payload_type - b->payload_type;

	if (!result) {
		result = a->type - b->type;
	}

	return result;
}

RB_GENERATE(niortp_transport_map, niortp_transport_entry, entry,
		niortp_transporter_compare);
